﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.BLL.Extend;
using ZX.Common;
using System.Data;


namespace ZX.BLL
{
    public partial class BUser : BLL.Extend.BaseBussnies
    {
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public BUser()
        {
            this.entity = new DatUser();
        }
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>返回字符串</returns>
        public override string CheckData(BaseEntity entity)
        {
            DatUser dtUser = entity as DatUser;
            if (string.IsNullOrEmpty(dtUser.LoginName))
            {
                return "用户名不能为空!";
            }
            else if (string.IsNullOrEmpty(dtUser.Password))
            {
                return "密码不能为空!";
            }
            return base.CheckData(dtUser);
        }
        /// <summary>
        /// 判断用户名是否已存在
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>true:已存在 false:不存在</returns>
        public bool IsExist(DatUser entity)
        {
            return GetEntityByCondition("LoginName='" + entity.LoginName + "'") != null;
        }
        /// <summary>
        /// 判断用户名是否已存在
        /// </summary>
        /// <param name="LoginName">用户名</param>
        /// <returns>true:已存在 false:不存在</returns>
        public bool IsExist(string LoginName)
        {
            return GetEntityByCondition("LoginName='" + LoginName + "'") != null;
        }
        /// <summary>
        /// 修改用户时判断用户名登录名是否已存在
        /// </summary>
        /// <param name="LoginName">用户名</param>
        /// <returns>true:已存在 false:不存在</returns>
        public bool IsExist(string LoginName, Guid userId)
        {
            return GetEntityByCondition("LoginName='" + LoginName + "' and UserID!='" + userId + "'") != null;
        }
        /// <summary>
        /// 用户登录时判断是否过期
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool IsExpire(DatUser user)
        {
            return GetEntityByCondition("LoginName='" + user.LoginName + "' and Password='" + user.Password + "' and IsExpire=1") != null;
        }
        /// <summary>
        /// 判断用户名和密码是否正确
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>true:成功 false:失败</returns>
        public DatUser GetDatUserByLoginNameAndPwd(DatUser entity)
        {
            return GetEntityByCondition("LoginName ='" + entity.LoginName + "' and Password='" + entity.Password + "'") as DatUser;
        }
        /// <summary>
        /// 切换用户
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns>true:成功 false:失败</returns>
        public DatUser GetDatUserByLoginName(DatUser entity)
        {
            return GetEntityByCondition("userid ='" + entity.UserID + "'") as DatUser;
        }

        
       
        /// <summary>
        /// 根据名称判断用户是否存在
        /// </summary>
        /// <param name="uName"></param>
        /// <returns></returns>
        public DatUser GetDatUserByUName(string uName)
        {
            return GetEntityByCondition("UName='" + uName + "'") as DatUser;
        }
        /// <summary>
        /// 判断用户名和密码是否正确
        /// </summary>
        /// <param name="Name">登录名</param>
        /// <param name="Password">密码</param>
        /// <returns></returns>
        public DatUser GetDatUserByLoginNameAndPwd(string  Name ,string Password)
        {
            DatUser dtusr = null;
            if (!string.IsNullOrEmpty(Name) && string.IsNullOrEmpty(Password)) 
            {
                dtusr = new DatUser();
                dtusr.LoginName = Name;
                dtusr.Password = ExtendMethods.GetMd5Hash(Password);
                dtusr= GetDatUserByLoginNameAndPwd(dtusr);
            
            }
            return dtusr;
        }
        /// <summary>
        /// 根据用户ID获取系统功能权限集合
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>返回系统功能权限集合</returns>
        public IList<SysFunction> GetFunctionByUserId(Guid userId)
        {
            return this.GetEntitysBySqlComm<SysFunction>("Select * from V_SysFunction where  UserID='" + userId + "' and (IsMenu<>'N' or IsMenu is Null)");
            //return GetEntitysByCondition("UserID='" + userId + "'") as IList<VSysfunction>;
        }

        /// <summary>
        /// 根据用户ID获取系统功能权限集合
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>返回系统功能权限集合</returns>
        public bool ExistFunction(Guid userId,Guid functionId)
        {
            var list = this.GetEntitysBySqlComm<SysFunction>("Select * from V_SysFunction where  UserID='" + userId + "' and functionId='"+functionId+"'");
            if (list == null)
            { return false; }
            if (list.Count == 0)
            { return false; }
            return true;
            //return GetEntitysByCondition("UserID='" + userId + "'") as IList<VSysfunction>;
        }
        /// <summary>
        /// 根据当前用户获取部门以及岗位    //todo zhuyun   20120711
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public DataTable GetDeptandDuty(string empid)
        {
            ZX.DAL.DBExtend.DbSql dbsql = new DAL.DBExtend.DbSql();
            string strsql = @"select b.DeptName,c.PostName from DatEmployee as a,DatDepartment as b,DatPost as c where a.EmpID='"+empid+"' and a.DeptID=b.DeptID and a.PostID=c.PostID";
            return dbsql.ExecuteQuery(strsql);
        }



        public IList<SysRoleFunction> GetRoleFunction(Guid? userID,Guid? functionID)
        {
            string strSql = @"SELECT   dbo.SysRoleFunction.*
                      FROM dbo.SysRoleFunction INNER JOIN
                      dbo.SysRole ON dbo.SysRoleFunction.RoleID = dbo.SysRole.RoleID INNER JOIN
                      dbo.SysUserRole ON dbo.SysRole.RoleID = dbo.SysUserRole.RoleID INNER JOIN
                      dbo.DatUser ON dbo.SysUserRole.UserID = dbo.DatUser.UserID
                      where dbo.DatUser.userID='{0}' and functionid='{1}'";
            return this.GetEntitysBySqlComm<SysRoleFunction>(string.Format(strSql, userID, functionID));
        }


        /// <summary>
        /// 根据用户ID获取用户角色集合
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>返回用户角色集合</returns>
        public IList<SysRole> GetUserRoleByUserId(Guid userId)
        {
            return GetEntitysBySqlComm<SysRole>("Select * from V_SysRole where UserID='" + userId + "'");
        }
        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>返回用户实体对象</returns>
        public DatUser GetDatUserByUserId(Guid userId)
        {
            return GetEntityByCondition<DatUser>("UserID='" + userId + "'") as DatUser;
        }
        /// <summary>
        /// 根据用户名获取用户实体对象
        /// </summary>
        /// <param name="name">用户名</param>
        /// <returns>用户实体对象</returns>
        public DatUser GetDatUserByName(string name)
        {
            return GetEntityByCondition<DatUser>("LoginName='" + name + "'") as DatUser;
        }
        /// <summary>
        /// 判断职员和人员是否已被选择
        /// </summary>
        /// <param name="CustomerID">客户ID</param>
        /// <param name="EmpID">职员ID</param>
        /// <param name="UserId">用户ID</param>
        /// <param name="IsOutsite">是否为外部用户</param>
        /// <returns></returns>
        public bool GetDatUserByID(Guid CustomerID, Guid EmpID, Guid UserId, bool IsOutsite)
        {
            bool temp = false;
            if (IsOutsite == true && CustomerID != new Guid("00000000-0000-0000-0000-000000000000"))  //是
            {
                temp = GetEntityByCondition("CustomerID='" + CustomerID + "' and UserID!='" + UserId + "'") != null;
            }
            else if (IsOutsite == false && EmpID != new Guid("00000000-0000-0000-0000-000000000000"))  //否
            {
                temp = GetEntityByCondition("EmpID='" + EmpID + "' and UserID!='" + UserId + "'") != null;
            }
            return temp;
        }
        /// <summary>
        /// 根据邮箱获取用户实体对象
        /// </summary>
        /// <param name="email">邮箱</param>
        /// <returns>用户实体对象</returns>
        public DatUser GetDatUserByEmail(string email)
        {
            return GetEntityByCondition<DatUser>("Email='" + email + "'") as DatUser;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="functionid"></param>
        /// <returns></returns>
        public String[] GetFunctions(string userid,string functionid)
        {

            var list = GetEntitys<UserFunction>(" UserID='" + userid + "' and FunctionID='" + functionid + "' and BarItems is not null");
            
            if (list == null || list.Count() ==0)
            {
                return "".Split(',');
            }
            else
            {
                return list.Select(p=>p.BarItems).ToArray();
            }

        }
    }
}
